layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
    	elem:'#startSingleTime',
        lang: 'en',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm:ss'
    });
    
    laydate.render({
    	elem:'#endSinglePutTime',
        lang: 'en',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm:ss'
    });

    /**
     * 入库单明细表管理
     */
    var StoragePutSingle = {
        tableId: "storagePutSingleTable"
    };

    /**
     * 初始化表格的列
     */
    StoragePutSingle.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'singleState', sort: true, title: 'Receipt Status', style:'font-weight: 600;',templet: '#singleStateTpl'},
            {field: 'stockId', sort: true, title: 'Inventory ID', style:'font-weight: 600;', hide: true},
            {field: 'recommend', sort: true, title: 'Receipt Batch', style:'font-weight: 600;'},
            {field: 'storageNumber', sort: true, title: 'Receipt Order No', style:'background-color: #ecf5ff;font-weight: 600;',width:140},
            {field: 'updateTime', sort: true, title: 'Receipt Time', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'positionNumber', sort: true, title: 'Location', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'trayNumber', sort: true, title: 'Tray', style:'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsname', sort: true, title: 'Name', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Code', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'Item No', style:'background-color: #dbf1e1;font-weight: 600;', totalRowText: 'Total:'},
            {field: 'putNum', sort: true, title: 'Planned Qty', totalRow: true, style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'realNum', sort: true, title: 'Actual Qty', style:'background-color: #dbf1e1;font-weight: 600;', totalRow: true},
            {field: 'singleDateofmanufacture', sort: true, title: 'Manufacturing Date', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'singleBatchnumber', sort: true, title: 'Batch', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'putWeight', sort: true, title: 'Weight', totalRow: true},
            {field: 'detailedPutvolume', sort: true, title: 'Volume', totalRow: true},
            {field: 'unitprice', sort: true, title: 'Unit Price'},
            {field: 'singleMoney', sort: true, title: 'Amount', totalRow: true},
            {field: 'singleLargeclass', title: 'Category'},
            {field: 'singleGoodstype', title: 'Type', hide: true},
            {field: 'goodsUnit', sort: true, title: 'Unit'},
            {field: 'singlePacking', title: 'Packaging'},
            {field: 'goodsSpecs', sort: true, title: 'Specs'},
            {align: 'center', toolbar: '#tableBarSingle', title: 'Action',fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    StoragePutSingle.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['singleState'] = $("#singleState").val();
        queryData['storageNumber'] = $("#storageNumber").val();
        queryData['singleBatchnumber'] = $("#singleBatchnumber").val();
        queryData['warehouseId'] = $("#warehouseId").val();
        queryData['startSingleTime'] = $("#startSingleTime").val();
        queryData['endSinglePutTime'] = $("#endSinglePutTime").val();
        table.reload(StoragePutSingle.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 导出excel按钮
     */
    StoragePutSingle.exportExcel = function () {
        var checkRows = table.checkStatus(StoragePutSingle.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("Please select data to export");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + StoragePutSingle.tableId,
        url: Feng.ctxPath + '/storagePutSingle/putjilu',
        page: true,
        limit: 120,
		limits:[60,120,240,360,480,1500,5000,10000,20000,80000],
        height: "full-60",
        cellMinWidth: 100,
        totalRow: true ,//开启合计行
        cols: StoragePutSingle.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        StoragePutSingle.search();
    });

    // 工具条点击事件
    table.on('tool(' + StoragePutSingle.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'exceptionHandling') {
            layer.open({
                type: 2,
                title: 'Exception Handling Records',
                area: ['1000px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/storagePutException?detailedId=' + data.id,
            });
        }
    });

    // 单击行事件
    table.on('row(' + StoragePutSingle.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + StoragePutSingle.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + StoragePutSingle.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    });

    // 导出excel
    $('#btnExp').click(function () {
        StoragePutSingle.exportExcel();
    });

    // 导出excel
    $('#btnExpBySpec').click(function () {
		window.location.href = Feng.ctxPath + '/storagePutSingle/exportExcelBySpec?singleState='+$("#singleState").val()
		+"&storageNumber="+$("#storageNumber").val()+"&condition2="+$("#condition").val()
		+"&singleBatchnumber="+$("#singleBatchnumber").val()+"&warehouseId="+$("#warehouseId").val()
		+"&startSingleTime="+$("#startSingleTime").val()+"&endSinglePutTime="+$("#endSinglePutTime").val();
    });

});
